import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hx/services/app_theme_services.dart';
import 'package:flutter_hx/theme/font_size.dart';
import 'package:flutter_hx/widgets/ez_image.dart';
import 'package:get/get.dart';

import 'address_edit_logic.dart';

class AddressEditPage extends GetView<AddressEditLogic> {
  const AddressEditPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          elevation: 0,
          automaticallyImplyLeading: true,
          iconTheme: const IconThemeData(color: Colors.black),
          centerTitle: true,
          title: Text("${controller.isAdd ? '新增' : '编辑'}收货地址",
              style: AppThemeServices.textTheme.appBarTitle),
        ),
        backgroundColor: const Color(0xFFF7F8FA),
        body: Stack(
          children: [
            Column(
              children: [
                ///上方 收货人 手机号码 所在地区 详细地址
                Container(
                  width: Get.width,
                  decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(7.5),
                          bottomRight: Radius.circular(7.5))),
                  padding: const EdgeInsets.symmetric(horizontal: 10)
                      .copyWith(bottom: 19),
                  child: GetBuilder<AddressEditLogic>(builder: (logic) {
                    return Column(
                      children: [
                        _inputItem(
                          label: "收货人",
                          placeholder: "请填写收货人",
                          onChanged: (v) => controller.name = v,
                          initialValue: controller.data.realName,
                        ),
                        _inputItem(
                            label: "手机号码",
                            placeholder: "请填写手机号",
                            onChanged: (v) => controller.phone = v,
                            keyboardType: TextInputType.number,
                            initialValue: controller.data.phone,
                            // maxLength: 11,
                            textInputFormatterList: [
                              FilteringTextInputFormatter.digitsOnly,
                              LengthLimitingTextInputFormatter(11)
                            ]),
                        _addressSelect(
                            label: "所在地区",
                            address: controller.addressStr,
                            onTap: () => controller.showCityPicker(context)),
                        _inputItem(
                          label: "详细地址",
                          placeholder: "请填写详细地址",
                          onChanged: (v) => controller.detail = v,
                          initialValue: controller.data.detailed,
                        ),
                      ],
                    );
                  }),
                ),
                const SizedBox(height: 10),
                ///去除默认
                // Container(
                //   decoration: BoxDecoration(
                //     borderRadius: BorderRadius.circular(7.5),
                //     color: Colors.white,
                //   ),
                //   padding: const EdgeInsets.symmetric(horizontal: 11),
                //   height: 51,
                //   alignment: Alignment.center,
                //   child: Row(
                //     children: [
                //       const Expanded(
                //         child: Text(
                //           "是否设为默认地址",
                //           style: TextStyle(
                //               fontSize: FontSize.pt13,
                //               color: Color(0xFF323233)),
                //         ),
                //       ),
                //       CustomSwitch(
                //         initFlag: controller.data.isDefault == "1",
                //         onTap: (animationController) {
                //           if (controller.data.isDefault == "1") {
                //             controller.data.isDefault = "0";
                //             animationController.reverse();
                //           } else {
                //             controller.data.isDefault = "1";
                //             animationController.forward();
                //           }
                //         },
                //       )
                //     ],
                //   ),
                // )
              ],
            ),
            Positioned(
                bottom: 20,
                child: SizedBox(
                  width: Get.width,
                  child: InkWell(
                    onTap: () => controller.saveAddress(),
                    child: GetBuilder<AddressEditLogic>(builder: (logic) {
                      return Container(
                        margin: const EdgeInsets.symmetric(horizontal: 10),
                        decoration: BoxDecoration(
                            color: controller.goodToSave
                                ? const Color(0xFF00C3B3)
                                : AppThemeServices.appColors.disabled,
                            borderRadius: BorderRadius.circular(45)),
                        height: 43,
                        child: const Center(
                          child: Text(
                            "保存",
                            style: TextStyle(
                                fontSize: FontSize.pt15, color: Colors.white),
                            textAlign: TextAlign.center,
                          ),
                        ),
                      );
                    }),
                  ),
                ))
          ],
        ),
      ),
    );
  }

  Widget _addressSelect({
    required String label,
    String? placeholder,
    Function? onTap,
    String? address,
  }) {
    return SizedBox(
      height: 51,
      child: Row(
        children: [
          ConstrainedBox(
            constraints: const BoxConstraints(minWidth: 74.5),
            child: Text(
              label,
              style: const TextStyle(
                  fontSize: FontSize.pt13, color: Color(0xFF323233)),
            ),
          ),
          Expanded(
              child: GestureDetector(
            onTap: () => onTap?.call(),
            behavior: HitTestBehavior.opaque,
            child: Row(
              children: [
                Expanded(
                  child: Text(
                    address ?? placeholder ?? "请选择所在地区",
                    style: TextStyle(
                      color: address == null
                          ? const Color.fromRGBO(150, 151, 153, 1)
                          : const Color(0xFF323233),
                      fontSize: FontSize.pt13,
                    ),
                  ),
                ),
                const EzImage.asset("icon_address_arrow_right.png")
              ],
            ),
          ))
        ],
      ),
    );
  }

  Widget _inputItem(
      {required String label,
      String? placeholder,
      List<TextInputFormatter>? textInputFormatterList,
      TextInputType? keyboardType,
      int? maxLength,
      String? initialValue,
      Function? onChanged}) {
    return SizedBox(
      height: 51,
      child: Row(
        children: [
          ConstrainedBox(
            constraints: const BoxConstraints(minWidth: 74.5),
            child: Text(
              label,
              style: const TextStyle(
                  fontSize: FontSize.pt13, color: Color(0xFF323233)),
            ),
          ),
          Expanded(
            child: TextFormField(
              initialValue: initialValue,
              maxLength: maxLength,
              onChanged: (v) => onChanged?.call(v),
              style: const TextStyle(fontSize: FontSize.pt13)
                  .copyWith(color: const Color(0xFF323233)),
              inputFormatters: textInputFormatterList,
              keyboardType: keyboardType,
              decoration: InputDecoration(
                  hintText: placeholder ?? "",
                  border: InputBorder.none,
                  hintStyle: const TextStyle(fontSize: FontSize.pt13)
                      .copyWith(color: const Color.fromRGBO(150, 151, 153, 1))),
            ),
          )
        ],
      ),
    );
  }
}
